define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mytask/index' + location.search,
                    add_url: 'mytask/add',
                    edit_url: 'mytask/edit',
                    del_url: 'mytask/del',
                    multi_url: 'mytask/multi',
                    import_url: 'mytask/import',
                    table: 'task',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible:false, title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'admin_id', title: __('Admin_id'), searchList: $.getJSON("project/admin"), operate: '=',formatter: function (value,row) {
                                return row.admin_id_text;
                            }},
                        {field: 'assign_id', visible:false, title: __('Assign_id'),searchList: $.getJSON("project/admin"),operate: '=',formatter: function (value,row) {
                                return row.assign_id_text;
                            }},
                        {field: 'project_ids', title: __('Project_ids'),searchList: $.getJSON("project/searchList"),operate: 'FIND_IN_SET',formatter: function (value,row) {
                                if(row.project_ids_text.length > 20){
                                    return row.project_ids_text.toString().substr(0, 20) + '...';
                                }else{
                                    return row.project_ids_text;
                                }
                            }
                        },
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', visible:false , title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh', visible:false, title: __('Weigh'), operate: false},
                        {field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {name: 'finish', text: __('完成'), title: __('完成'), icon: 'fa fa-check', classname: 'btn btn-xs btn-info btn-dialog btn-ajax', url: 'mytask/finish', 'confirm': '此操作不可逆，确认完成吗？', refresh:true},
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function(value,row,index){
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                if(row.state == 1){
                                    $(table).data("operate-finish",null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'mytask/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'mytask/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'mytask/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        finish: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
